package hyung.jin.seo;

import hyung.jin.seo.model.NumberBasket;
import hyung.jin.seo.util.LottoConstants;
import hyung.jin.seo.util.Utility;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity 
{
	private EditText[] num;
	private Button generateB;
	private Button clearB;
	
	
//	private String[] generatedNum;
	private NumberBasket basket;
	private Utility util;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        num = new EditText[LottoConstants.NUMBER_BALL];
        util = Utility.getInstance();
        basket = new NumberBasket();
        num[0] = (EditText) findViewById(R.id.num0);
        num[1] = (EditText) findViewById(R.id.num1);
        num[2] = (EditText) findViewById(R.id.num2);
        num[3] = (EditText) findViewById(R.id.num3);
        num[4] = (EditText) findViewById(R.id.num4);
        num[5] = (EditText) findViewById(R.id.num5);
        generateB = (Button) findViewById(R.id.generateB);
        clearB = (Button) findViewById(R.id.clearB);
        
        generateB.setOnClickListener(new View.OnClickListener() {

        	public void onClick(View v) 
			{
        		
        		if(isAlreadyWritten())
        		{
        			Toast.makeText(Main.this, "Please try again after clearing number", Toast.LENGTH_LONG).show();
        			return;
        		}
        		basket = util.generateNumber(prepareBasket(basket));
				assignNums();
			}
		});
        
        clearB.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) 
			{
				clearNums();
			}

		});
    }
    
    private void assignNums() 
    {
    	if(num!=null)
    	{
    		String[] returnedNum = basket.getNumbers();
    		boolean[] isSelected = basket.getSelected();
    		for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
    		{
    			num[i].setText(padding(returnedNum[i]));
    			if(isSelected[i])
    			{
    				// differenciate color in EditText
    				num[i].setTextColor(Color.RED);
    			}
    		}
    	}
	}
    
	private void clearNums() 
	{
		if(num!=null)
		{
			for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
			{
				num[i].setText(LottoConstants.EMPTY_NUMBER);
				num[i].setTextColor(Color.BLACK);
			}
		}
	}
	
	private String padding(String num)
	{
		if(num.length()==1)
		{
			return " "+num;
		}else{
			return num;
		}
	}
	
	private boolean isAlreadyWritten() 
	{
		boolean isWritten = !(num[0].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER))
							&& !(num[1].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER))
							&& !(num[2].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER))
							&& !(num[3].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER))
							&& !(num[4].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER))
							&& !(num[5].getText().toString().equalsIgnoreCase(LottoConstants.EMPTY_NUMBER));
		if(isWritten)
		{
			// all already written so just guide cleaning up and try again
			return true;
		}else{
			// prepare sending basket with already written numbers
			return false;
		}
	}
	
	private NumberBasket prepareBasket(NumberBasket basket) 
	{
		String[] textValues = new String[LottoConstants.NUMBER_BALL];
		for(int i=0; i<LottoConstants.NUMBER_BALL; i++)
		{
			textValues[i] = num[i].getText().toString();
			num[i].setTextColor(Color.BLACK);
		}
		boolean[] selectedValues = new boolean[LottoConstants.NUMBER_BALL];
		basket.setNumbers(textValues);
		basket.setSelected(selectedValues);
		return basket;
	}

}